package com.tiantianfresh.admin.controller;

import com.tiantianfresh.model.pojo.AJAXResult;
import com.tiantianfresh.model.pojo.Role;
import com.tiantianfresh.model.pojo.User;
import com.tiantianfresh.admin.service.RoleService;
import com.tiantianfresh.admin.service.UserService;
import com.tiantianfresh.admin.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author FANGAO
 * @date 2021/11/11 - 上午 10:38
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @ResponseBody
    @RequestMapping("/index")
    public Object index(String queryText,Integer currentPageNo,Integer pageSize,Integer identity){
        AJAXResult ajaxResult = new AJAXResult();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("queryText",queryText);
        map.put("identity",identity);
        Page<User> page = new Page<User>(pageSize,currentPageNo,userService.userTotalCount(map));
        map.put("currentPageNo",page.getCurrentPageNo());
        map.put("pageSize",page.getPageSize());
        page.setDatas(userService.getListUser(map));
        if (page.getDatas()!=null) ajaxResult.setSuccess(true);
        ajaxResult.setData(page);
        return ajaxResult;
    }

    @RequestMapping("/add")
    public String add(){
        return "user/add";
    }
    @ResponseBody
    @RequestMapping("/insert")
    public Object insert(User user){
        user.setUserpswd("123456");
        user.setCreateTime(new Date());
        AJAXResult ajaxResult = new AJAXResult();
        ajaxResult.setSuccess(userService.userInsert(user));
        return ajaxResult;
    }
    @RequestMapping(value ="/edit/{id}")
    public String goEdit(@PathVariable Integer id, Model model){
        User user = userService.queryByid(id);
        model.addAttribute("user",user);
        return "user/edit";
    }
    @ResponseBody
    @RequestMapping("/update")
    public Object update(User user){
        AJAXResult ajaxResult = new AJAXResult();
        ajaxResult.setSuccess(userService.userUpdate(user));
        return ajaxResult;
    }
    @ResponseBody
    @RequestMapping("/delete")
    public Object delete(Integer id){
        AJAXResult ajaxResult = new AJAXResult();
        ajaxResult.setSuccess(userService.userDelete(id));
        return ajaxResult;
    }
    @ResponseBody
    @RequestMapping("/deleteUsers")
    //<td><input type="checkbox" name="userid" value="'+user.id+'"></td>
    //根据前端input的name属性取值-->Integer[] userid
    public Object deleteUsers(Integer[] userid){
        AJAXResult ajaxResult = new AJAXResult();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("userids",userid);
        ajaxResult.setSuccess(userService.deleteUsers(map));
        return ajaxResult;
    }
    @RequestMapping("/assign/{id}")
    public String assign(@PathVariable Integer id,Model model){
        model.addAttribute("user",userService.queryByid(id));
        List<Role> roles = roleService.getRoles();
        //用户未分配的角色
        List<Role> unassingedRoles =new ArrayList<Role>();
        //用户已分配的角色
        List<Role> assingedRoles =new ArrayList<Role>();
        //获取关系表的数据
        List<Integer> roleids = userService.getRoleids(id);
        for (Role role : roles) {
            //roleids.contains(role.getId())是否包含（）中的内容
            if (roleids.contains(role.getId())){
                assingedRoles.add(role);
            }else{
                unassingedRoles.add(role);
            }
        }
        model.addAttribute("unassingedRoles",unassingedRoles);
        model.addAttribute("assingedRoles",assingedRoles);
        return "user/assign";
    }
    @ResponseBody
    @RequestMapping("/doAssign")
    public Object doAssign(Integer userid,Integer[] unassignroleids){
        AJAXResult result = new AJAXResult();
        try {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("userid",userid);
            map.put("roleids",unassignroleids);
            result.setSuccess(userService.addUserAndRole(map));
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/dounAssign")
    public Object dounAssign(Integer userid,Integer[] assignroleids){
        AJAXResult result = new AJAXResult();
        try {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("userid",userid);
            map.put("roleids",assignroleids);
            result.setSuccess(userService.deleteUserAndRole(map));
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/updateuser")
    public Object updateuser(User user){
        System.out.println("user----------->"+user);
        AJAXResult result = new AJAXResult();
        result.setSuccess(userService.updateuser(user));
        return result;
    }
}
